<template>
  <div>
    <el-form ref="form" :model="form" :rules="rulesForm" label-width="130px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" style="width:80%" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择">
          <el-option v-for="data in emparr" :key="data.id" :label="data.username" :value="data.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" />
      </el-form-item>
    </el-form>

    <span slot="footer" style="display:block;text-align:center">
      <el-button>取 消</el-button>
      <el-button type="primary" @click="addorEdit">确 定</el-button>
    </span>
  </div>
</template>
<script>
import { getSimple, addDepartment, getDepartmentById, editDepartmentById } from '@/api/department'
export default {
  name: '',
  props: {
    id: {
      type: String,
      default: ''
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      emparr: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: this.id
      },
      rulesForm: {
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入部门编码', trigger: 'blur' }],
        manager: [{ required: true, message: '请选择部门负责人', trigger: 'change' }],
        introduce: [{ required: true, message: '请输入部门介绍', trigger: 'blur' }]
      }
    }
  },
  computed: {
  },
  watch: {
    isEdit: {
      immediate: true,
      async handler(edit) {
        if (edit) {
          const res = await getDepartmentById(this.id)
          console.log(res)
          if (res.code === 10000) {
            this.form = res.data
          } else {
            console.log('查询失败')
          }
        }
      }
    }
  },
  created() {
    this.getDepartmenter()
  },
  methods: {
    async getDepartmenter() {
      const res = await getSimple()
      if (res.code === 10000) {
        this.emparr = res.data
      }
    },
    addorEdit() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        console.log(this.form)
        if (this.isEdit) {
          const res = await editDepartmentById(this.form)
          if (res.code === 10000) {
            this.$message({
              type: 'success',
              message: res.message
            })
          } else {
            this.$message({
              type: 'error',
              message: '操作失败，请联系管理员'
            })
          }
        } else {
          const res = await addDepartment(this.form)
          if (res.code === 10000) {
            this.$message({
              type: 'success',
              message: res.message
            })
          } else {
            this.$message({
              type: 'error',
              message: '操作失败，请联系管理员'
            })
          }
        }
        //   让父组件关闭弹窗
        this.$emit('success')
      })
    }
  }
}
</script>
<style lang='less'  scoped>

</style>
